<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

use think\facade\Route;

Route::pattern('id', '\d+');

Route::get('/lo', 'api/Index/index');

/**
 * 后台管理模块
 */
Route::group('admin', function(){
    // 登录模块
    Route::group('login', function(){
        Route::get('/', 'admin/Login/login');
        Route::post('input', 'admin/Login/input');
        Route::get('logout', 'admin/Login/logout');
    });
    Route::get('qiniuToken', 'admin/Common/qiniuToken');
    Route::group('', function(){
        //rbac
        Route::group('rbac', function(){
            Route::get('table', function(){
                (new \gmars\rbac\Rbac())->createTable();
            });
        });
        //首页
        Route::get('/', 'admin/Index/index');
        Route::get('welcome', 'admin/Index/welcome');
        Route::get('icon', 'admin/Index/getIcon');
        //公共操作
        Route::controller('common', 'admin/Common');
        // 管理员模块
        Route::group('admin', function(){
            Route::get('admin/skin/:skin','admin/admin.Administrator/skin')->validate('app\admin\validate\Admin', 'skin');
            Route::get('admin/status/:id', 'admin/admin.Administrator/status')->model('\app\admin\model\admin\Admin');
            Route::get('admin/only', 'admin/admin.Administrator/only');
            Route::delete('admin/destory', 'admin/admin.Administrator/destory');
            Route::resource('admin', 'admin/admin.Administrator')->model('\app\admin\model\admin\Admin');
            Route::get('permission/status/:id', 'admin/admin.Permission/status')->model('\app\admin\model\admin\Permission');
            Route::get('permission/auto', 'admin/admin.Permission/auto');
            Route::delete('permission/destory', 'admin/admin.Permission/destory');
            Route::resource('permission', 'admin/admin.Permission');
            Route::get('permissionCategory/status/:id', 'admin/admin.PermissionCategory/status')->model('\app\admin\model\admin\PermissionCategory');
            Route::get('permissionCategory/only', 'admin/admin.PermissionCategory/only');
            Route::delete('permissionCategory/destory', 'admin/admin.PermissionCategory/destory');
            Route::resource('permissionCategory', 'admin/admin.PermissionCategory');
            Route::get('roles/status/:id', 'admin/admin.Roles/status')->model('\app\admin\model\admin\Role');
            Route::delete('roles/destory', 'admin/admin.Roles/destory');
            Route::resource('roles', 'admin/admin.Roles');
            Route::get('menus/status/:id', 'admin/admin.Menus/status')->model('\app\admin\model\admin\Menus');
            Route::get('menus/only', 'admin/admin.Menus/only');
            Route::delete('menus/destory', 'admin/admin.Menus/destory');
            Route::resource('menus', 'admin/admin.Menus')->model('\app\admin\model\admin\Menus');
        });
        //点读机模块
        Route::group('erm', function(){
            Route::get('sidebar/status/:id', 'admin/erm.Sidebar/status')->model('\app\admin\model\erm\Sidebar');
            Route::get('sidebar/only', 'admin/erm.Sidebar/only');
            Route::delete('sidebar/destory', 'admin/erm.Sidebar/destory');
            Route::resource('sidebar', 'admin/erm.Sidebar')->model('\app\admin\model\erm\Sidebar');
            Route::get('books/status/:id', 'admin/erm.Books/status')->model('\app\admin\model\erm\Books');
            Route::get('books/only', 'admin/erm.Books/only');
            Route::delete('books/destory', 'admin/erm.Books/destory');
            Route::resource('books', 'admin/erm.Books')->model('\app\admin\model\erm\Books');
            Route::get('booksContents/status/:id', 'admin/erm.booksContents/status')->model('\app\admin\model\erm\BooksContents');
            Route::delete('booksContents/destory', 'admin/erm.booksContents/destory');
            Route::resource('booksContents', 'admin/erm.BooksContents')->model('\app\admin\model\erm\BooksContents');
        });
        //用户模块
        Route::group('user', function(){
            Route::get('info/status/:id', 'admin/user.Info/status')->model('\app\admin\model\user\UserNumber');
            Route::get('info/only', 'admin/user.Info/only');
            Route::resource('info', 'admin/user.Info')->model('\app\admin\model\user\UserNumber');
        });
        //客服模块
        Route::group('customer', function(){
            Route::resource('message', 'admin/customer.Message');
        });
    })->middleware([
        \app\http\middleware\IfLogin::class,
        \app\http\middleware\AuthUpdate::class
    ]);
});

//api接口
Route::group('v1', function(){
    //用户组
    Route::group('user', function(){
        Route::get('oauth2', 'api/user.OAuth2/logonCredentials' );
        Route::group('', function(){
            Route::post('addInfo', 'api/user.OAuth2/addInfo');
            Route::get('language/:language', 'api/user.Info/language')
                ->pattern(['language'=>'\d+'])
                ->validate('app\api\validate\user\UserNumber', 'language');
            Route::post('manager/set', 'api/user.UserManager/set')->validate('app\api\validate\user\UserManager', 'set');
            Route::post('manager/score', 'api/user.UserManager/score')->validate('app\api\validate\user\UserManager', 'score');
        })->middleware([
            \app\http\middleware\CheckToken::class
        ]);
    });
    Route::group('', function(){
        //api接口
        Route::group('api', function(){
            //书架组
            Route::group('bookshelf', function () {
                Route::get('books/:sid', 'api/bookshelf.Books/index')->pattern(['sid' => '\d+']);
                Route::get('book/read/:id', 'api/bookshelf.Books/read');
                Route::get('search/:search', 'api/bookshelf.Books/search');
            });
            //栏目组
            Route::group('columns', function () {
                Route::get('sidebar', 'api/columns.Sidebar/index');
            });
            //system
            Route::group('system', function(){
                Route::post('feedback', 'api/system.Feedback/send');
                Route::get('read/:title', 'api/system.System/read')->pattern(['title'=>'\w+'])->model('title','app\api\model\system\System');
            });
            // server
            Route::group('server', function(){
                Route::post('tencentcloud/getAuthorization', 'api/server.Tencentcloud/getAuthorization');
            });
        });
    })->middleware([
        \app\http\middleware\CheckToken::class
    ]);
});

Route::post('location', 'api/Index/location')
    ->header('Access-Control-Allow-Origin','*')
    ->header('Access-Control-Allow-Credentials', 'true')
    ->allowCrossDomain();

/**
 * 命令行相关
 */
Route::group('console', function(){
    Route::get('call/:password', function($password){
        if ( $password !== 'administrator' ) return notFound();
        $callRes[] = \think\Console::call('clear')->fetch();
        $callRes[] = \think\Console::call('optimize:schema')->fetch();
        $callRes[] = \think\Console::call('optimize:config')->fetch();
        $callRes[] = \think\Console::call('optimize:route')->fetch();
        return json($callRes);
    });
    Route::get('clear/:password',function($password){
        if ( $password !== 'administrator' ) return notFound();
        return \think\Console::call('clear')->fetch();
    });
});

/**
 * 队列相关
 */
Route::group('job', function(){
    Route::get('pull', function(){
        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
        //   ( jobData 为对象时，存储其public属性的键值对 )
        $jobData = [ 'ts' => time(), 'bizId' => uniqid() , 'a' => 1 ];
        //定义消息队列名称,该名称与处理的类有映射关系
        $messageName = 'test';
        // 4.将该任务推送到消息队列，等待对应的消费者去执行
        $isPushed = \app\admin\service\Job::push( $jobData, $messageName );
        if( $isPushed !== false ){
            return accepted('请求已被接收,请等待处理.随机字符串:'.$isPushed);
        }else{
            return failed('消息推送失败');
        }
    });
});

return [

];
